using System.Net.Sockets;
using System.Net;

namespace WpfApp_ChenxiConsole.TCP
{
    /// <summary>
    /// 表示TCP客户端的信息类
    /// </summary>
    public class TcpClientInfo
    {
        /// <summary>
        /// 客户端连接的TcpClient对象
        /// </summary>
        public TcpClient Client { get; set; }

        /// <summary>
        /// 客户端的IP地址和端口
        /// </summary>
        public string? IpPort { get; set; }

        /// <summary>
        /// 客户端连接的网络流
        /// </summary>
        public NetworkStream Stream { get; set; }

        /// <summary>
        /// 客户端连接时间
        /// </summary>
        public DateTime ConnectTime { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="client">TcpClient对象</param>
        public TcpClientInfo(TcpClient client)
        {
            Client = client;
            Stream = client.GetStream();
            ConnectTime = DateTime.Now;
            
            // 获取客户端的IP地址和端口
            if (client.Client.RemoteEndPoint is IPEndPoint remoteEndPoint)
            {
                IpPort = $"{remoteEndPoint.Address}:{remoteEndPoint.Port}";
            }
        }

        /// <summary>
        /// 返回客户端信息的字符串表示
        /// </summary>
        /// <returns>格式化的客户端信息字符串</returns>
        public override string ToString()
        {
            return $"客户端 {IpPort} : 连接于 {ConnectTime.ToString("yyyy-MM-dd HH:mm:ss")}";
        }
    }
}